package com.xhwl.centiotdata.suppliersystem.mili;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.MiLiConstant;
import com.xhwl.centiotdata.pojo.dto.mili.*;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IOrganizationService;
import com.xhwl.centiotdata.service.cent.IResidentInfoService;
import com.xhwl.centiotdata.utils.RedisUtils;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.pedestrian.DoorActionEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.common.pojo.cent.sdata.Room;
import com.xhwl.common.query.cent.sdata.RoomQuery;
import com.xhwl.common.utils.ChkUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: wuyuhao
 * @createDate: 2022/10/18 14:08
 */
@Service
@Slf4j
public class PedestrianMiLiHandler {

    @Autowired
    private GeneralMiLiHandler generalMiLiHandler;

    @Autowired
    private IResidentInfoService iResidentInfoService;

    @Autowired
    private IOrganizationService iOrganizationService;

    @Autowired
    private RedisUtils redisUtils;

    public List<PedestrianRecordMiLiDTO> pullRecord(PedestrianIntegrationConfigDTO configDTO, Date date) {
        if(date == null){
            date = new Date();
        }
        //计算开始时间（拉取前1小时的数据）
        Date startTime = DateUtil.offset(date, DateField.HOUR_OF_DAY, -1);

        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject();
        req.put("EventType", 0);
        req.put("DeviceNo", "");
        req.put("TeneName", "");
        req.put("PersonDeviceDesc", "");
        req.put("StartTime", DateUtil.formatDateTime(startTime));
        req.put("EndTime", DateUtil.formatDateTime(date));
        req.put("PageIndex", 1);
        req.put("CountPrePage", 500);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.ENTRANCE_LOG_URL, req);
        // 解析返回的数据
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.ENTRANCE_LOG_URL, res.getString("Message"));
            return null;
        }
        JSONObject body = res.getJSONObject("Body");
        JSONArray list = body.getJSONArray("List");
        return list.toJavaList(PedestrianRecordMiLiDTO.class);
    }

    public ResultJson openDoor(PedestrianIntegrationConfigDTO configDTO, DeviceDTO deviceDTO, DoorActionEnum doorAction) {
        String url;
        switch (doorAction) {
            case REMOTELY_OPEN:
                url = MiLiConstant.CTRL_DOOR_OPEN_URL;
                break;
            default:
                return ResultJson.fail("无对应设备操作");
        }
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject();
        req.put("DeviceNo", deviceDTO.getThirdDeviceId());
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), url, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", url, res.getString("Message"));
            return ResultJson.fail("操作失败");
        }
        return ResultJson.success();
    }

    public List<TenementMiLiDTO> queryTenement(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.TENEMENT_QUERY_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.TENEMENT_QUERY_URL, res.getString("Message"));
            return null;
        }
        JSONObject body = res.getJSONObject("Body");
        JSONArray list = body.getJSONArray("List");
        return list.toJavaList(TenementMiLiDTO.class);
    }

    public TenementMiLiDTO addTenement(PedestrianIntegrationConfigDTO configDTO, TenementMiLiDTO tenementMiLiDTO) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = JSON.parseObject(JSON.toJSONString(tenementMiLiDTO));
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.TENEMENT_ADD_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.TENEMENT_ADD_URL, res.getString("Message"));
            return null;
        }
        JSONObject body = res.getJSONObject("Body");
        return body.toJavaObject(TenementMiLiDTO.class);
    }

    public boolean modifyTenement(PedestrianIntegrationConfigDTO configDTO, TenementMiLiDTO tenementMiLiDTO) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = JSON.parseObject(JSON.toJSONString(tenementMiLiDTO));
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.TENEMENT_MODIFY_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.TENEMENT_MODIFY_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public boolean delTenement(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.TENEMENT_DELETE_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.TENEMENT_DELETE_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public List<TenementMiLiDTO> infoTenement(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.TENEMENT_INFO_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.TENEMENT_INFO_URL, res.getString("Message"));
            return null;
        }
        log.info("米立人员信息： 参数： {} ， 结果： {}", req.toJSONString() ,res.toJSONString());
        JSONArray body = res.getJSONArray("Body");
        return body.toJavaList(TenementMiLiDTO.class);
    }

    /**
     * 住户启用/停用门禁设备的通行权限
     *
     * @param configDTO
     * @param map
     */
    public boolean permissionTenement(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.TENEMENT_PERMISSION_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("参数: {}", req.toJSONString());
            log.error("请求url: {}, errMsg: {}", MiLiConstant.TENEMENT_PERMISSION_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    /**
     * 控制住户通行时效，如住户物业欠费交费完后，应当调用该接口延长有效期
     *
     * @param configDTO
     * @param map
     */
    public boolean setEffectiveDataTenement(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.TENEMENT_SET_EFFECTIVE_DATA_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("参数: {}", req.toJSONString());
            log.error("请求url: {}, errMsg: {}", MiLiConstant.TENEMENT_SET_EFFECTIVE_DATA_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public WorkerMiLiDTO addWorker(PedestrianIntegrationConfigDTO configDTO, WorkerMiLiDTO workerMiLiDTO) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = JSON.parseObject(JSON.toJSONString(workerMiLiDTO));
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.WORKER_ADD_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.WORKER_ADD_URL, res.getString("Message"));
            return null;
        }
        JSONObject body = res.getJSONObject("Body");
        return body.toJavaObject(WorkerMiLiDTO.class);
    }

    public boolean modifyWorker(PedestrianIntegrationConfigDTO configDTO, WorkerMiLiDTO workerMiLiDTO) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = JSON.parseObject(JSON.toJSONString(workerMiLiDTO));
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.WORKER_MODIFY_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.WORKER_MODIFY_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public boolean delWorker(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.WORKER_DELETE_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.WORKER_DELETE_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public List<WorkerMiLiDTO> queryWorker(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.WORKER_QUERY_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.WORKER_QUERY_URL, res.getString("Message"));
            return null;
        }
        JSONObject body = res.getJSONObject("Body");
        JSONArray list = body.getJSONArray("List");
        return list.toJavaList(WorkerMiLiDTO.class);
    }

    public void permissionWorker(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.WORKER_PERMISSION_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.WORKER_PERMISSION_URL, res.getString("Message"));
            throw new BusinessException(res.getString("Message"));
        }
    }

    /**
     * 对住户新增卡，即发卡，默认通行权限：本社区所有区口，外加本住户所在梯口权限
     *
     * @param configDTO
     * @param map
     */
    public boolean addCard(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.CARD_ADD_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.CARD_ADD_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public void replaceCard(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.CARD_REPLACE_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.CARD_REPLACE_URL, res.getString("Message"));
            throw new BusinessException(res.getString("Message"));
        }
    }

    /**
     * 删除住户卡，删卡后权限也跟着停用
     *
     * @param configDTO
     * @param map
     */
    public boolean delCard(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.CARD_DELETE_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.CARD_DELETE_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public boolean addFace(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.FACE_ADD_URL, req);
        JSONObject res = JSON.parseObject(response);
        log.info("米立添加人脸结果： {}", res.toJSONString());
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.FACE_ADD_URL, res.getString("Message"));
            return false;
        }
        log.info("===url： {}   ===参数： {}     ===结果： {}", MiLiConstant.FACE_ADD_URL, req.toJSONString(), res.toJSONString());
        return true;
    }

    public boolean delFace(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.FACE_DELETE_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.FACE_DELETE_URL, res.getString("Message"));
            return false;
        }
        return true;
    }

    public boolean removeFaceById(PedestrianIntegrationConfigDTO configDTO, Map<String, Object> map) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());
        JSONObject req = new JSONObject(map);
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.FACE_REMOVE_BY_ID_URL, req);
        JSONObject res = JSON.parseObject(response);
        if (res.getInteger("Result") != 0) {
            log.error("请求url: {}, errMsg: {}", MiLiConstant.FACE_REMOVE_BY_ID_URL, res.getString("Message"));
            return false;
        }
        return true;
    }


    public ResultJson getQrCode(PedestrianIntegrationConfigDTO configDTO, Account account) {
        LoginMiLiDTO loginInfo = generalMiLiHandler.getLoginInfo(configDTO.getLoginName(),
                configDTO.getPassword(), configDTO.getIpAndPort());

        // 查询社区框架号
        String communityId = queryFrameNo(configDTO, loginInfo);

        // 查询被访人房号
        String frameNo = queryUserInfo(configDTO, account);
        if(frameNo == null) {
            return ResultJson.fail("该住户没有绑定房屋，无法获取二维码");
        }

        // 获取二维码
        String url = "https://yun.miligc.com/qrcode-generate/1.0/getqrcode";
        String timestamp = String.valueOf(System.currentTimeMillis()/1000);
        String appSecret = configDTO.getSk();
        String appId = configDTO.getAk();
        String verify = MD5.create().digestHex(timestamp + "|" + 3 + "|" + appSecret);
        String authorization = appId + ":" + timestamp + ":" + verify + ":" + ":" + 3;

        Date now = new Date();
        Map<String, Object> map = new HashMap<>();
        // 社区号+房号，例如：1-01010102
        map.put("userRoom", communityId + "-" + frameNo);
        map.put("beginTime", DateUtil.formatDateTime(now));
        map.put("endTime", DateUtil.formatDateTime(DateUtil.endOfDay(now)));
        map.put("times", 10);
        if(account != null) {
            map.put("userName", account.getName());
        }
        // 1为业主，2为访客
        map.put("userType", 1);

        String result = HttpRequest.post(url)
                .header("Authorization", "Basic " + Base64.encode(authorization))
                .header("Content-Type", "multipart/form-data")
                .form(map)
                .execute()
                .body();
        JSONObject str = JSON.parseObject(result);
        if(Integer.valueOf(1).equals(str.getInteger("errorCode"))) {
            Object qrcode = str.getJSONObject("body").get("qrcode");
            redisUtils.setWithMinute(String.format(MiLiConstant.TENEMENT_QRCODE_KEY, account.getId()), qrcode, 10);
            return ResultJson.success(qrcode);
        } else {
            log.error("获取二维码错误： {}", str.get("errorMsg"));
            throw new BusinessException();
        }
    }

    private String queryFrameNo(PedestrianIntegrationConfigDTO configDTO, LoginMiLiDTO loginInfo) {
        // 查询社区框架号
        String response = generalMiLiHandler.doBusinessRequest(configDTO.getLoginName(), loginInfo,
                configDTO.getIpAndPort(), MiLiConstant.FRAME_QUERY_URL, new JSONObject());
        log.info("queryFrame->response: {}", response);
        // 解析返回的设备列表数据
        JSONObject jsonObject = JSON.parseObject(response);
        JSONArray body = jsonObject.getJSONArray("Body");
        List<FrameMiLiDTO> frameMiLiDTOList = body.toJavaList(FrameMiLiDTO.class);
        if(ChkUtil.isNull(frameMiLiDTOList)) {
            log.error("社区为空： {}", body);
            throw new BusinessException();
        }
        FrameMiLiDTO frameMiLiDTO = frameMiLiDTOList.get(0);
        return frameMiLiDTO.getFrameNo();
    }

    private String queryUserInfo(PedestrianIntegrationConfigDTO configDTO, Account account) {
        if(ChkUtil.isNull(account)) {
            log.error("人员不存在：{}", account);
            throw new BusinessException();
        }
        String accountId = String.valueOf(account.getId());
        List<Organization> organizationList = iResidentInfoService.getOrgListByAccountId(accountId, configDTO.getProjectId());
        StringBuilder frameNo = new StringBuilder();
        if(ChkUtil.isNotNull(organizationList)) {
            List<Integer> orgIdList = organizationList.stream().map(Organization::getId).collect(Collectors.toList());
            RoomQuery roomQuery = new RoomQuery();
            roomQuery.setOrgIds(orgIdList);
            roomQuery.setProjectId(configDTO.getProjectId());
            List<Room> roomList = iOrganizationService.list(roomQuery);
            List<String> roomCode = roomList.stream().map(Room::getCode).collect(Collectors.toList());
            Map<String, String> codeMap = new HashMap<>();
            for(String code : roomCode) {
                String pre = code.substring(0, 4);
                String tail = code.substring(4);
                if(codeMap.get(pre) == null) {
                    codeMap.put(pre, tail);
                } else {
                    codeMap.put(pre, codeMap.get(pre) + "," + tail);
                }
            }
            for(Map.Entry entry : codeMap.entrySet()) {
                frameNo.append(entry.getKey()).append(entry.getValue()).append("&");
            }
            return frameNo.substring(0, frameNo.length() - 1);
        }
        return null;
    }
}
